﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HaumDisplay
{
    public class HaumImage : IDisposable
    {
        /// <summary>
        /// 图像
        /// </summary>
        public Bitmap bmp;

        /// <summary>
        /// 图像长宽
        /// </summary>
        public Size size;

        /// <summary>
        /// 角度
        /// </summary>
        public ushort rotate;

        /// <summary>
        /// 像素
        /// </summary>
        public int pixel;

        /// <summary>
        /// 像素格式
        /// </summary>
        public PixelFormat pixelFormat;

        /// <summary>
        /// 位大小
        /// </summary>
        public short bitDepth;

        /// <summary>
        /// 文件名
        /// </summary>
        public string fileName;

        /// <summary>
        /// 图像类型
        /// </summary>
        public ImageFormat imageFormat;

        /// <summary>
        /// 文件全路径
        /// </summary>
        public string fullPath;

        /// <summary>
        /// 时间
        /// </summary>
        public DateTime time;

        /// <summary>
        /// 空间大小 MB
        /// </summary>
        public double packageSize;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="image">图像</param>
        /// <param name="path">路径 默认""</param>
        public HaumImage(Image image, string path = "")
        {
            //图像
            this.bmp = (Bitmap)image.Clone();
            //宽高
            this.size = this.bmp.Size;
            //像素
            this.pixel = this.bmp.Width * this.bmp.Height;
            //像素格式
            this.pixelFormat = this.bmp.PixelFormat;
            //位深度
            switch (this.bmp.PixelFormat)
            {
                case PixelFormat.Format1bppIndexed: this.bitDepth = 1; break;
                case PixelFormat.Format4bppIndexed: this.bitDepth = 4; break;
                case PixelFormat.Format8bppIndexed: this.bitDepth = 8; break;
                case PixelFormat.Format16bppRgb555:
                case PixelFormat.Format16bppRgb565:
                case PixelFormat.Format16bppGrayScale: this.bitDepth = 16; break;
                case PixelFormat.Format24bppRgb: this.bitDepth = 24; break;
                case PixelFormat.Format32bppRgb:
                case PixelFormat.Format32bppArgb:
                case PixelFormat.Format32bppPArgb: this.bitDepth = 32; break;
                case PixelFormat.Format48bppRgb: this.bitDepth = 48; break;
                case PixelFormat.Format64bppArgb:
                case PixelFormat.Format64bppPArgb: this.bitDepth = 64; break;
                default: this.bitDepth = 1; break;
            }

            if (path == "")
            {
                this.fileName = "NAN";
                //图像全路径
                this.fullPath = "NAN";
            }
            else
            {
                //获取不带扩展名的文件名
                this.fileName = Path.GetFileNameWithoutExtension(path);
                //图像全路径
                this.fullPath = path;
            }
            //获取图像类型
            if (image.RawFormat.Equals(ImageFormat.Jpeg)) this.imageFormat = ImageFormat.Jpeg;
            else if (image.RawFormat.Equals(ImageFormat.Png)) this.imageFormat = ImageFormat.Png;
            else if (image.RawFormat.Equals(ImageFormat.Bmp)) this.imageFormat = ImageFormat.Bmp;
            else if (image.RawFormat.Equals(ImageFormat.Gif)) this.imageFormat = ImageFormat.Gif;
            else if (image.RawFormat.Equals(ImageFormat.Tiff)) this.imageFormat = ImageFormat.Tiff;
            else { this.imageFormat = null; }
            //时间
            this.time = DateTime.Now;
            //图像大小
            int pixelFormatSize = Image.GetPixelFormatSize(this.pixelFormat);
            int pixelLineSize = ((this.bmp.Width * pixelFormatSize + 31) / 32) * 4;
            long bmpBytes = (long)(pixelLineSize * this.bmp.Height);
            float objectOverhead = 24; // 对象和字段开销（近似）
            float gdiOverhead = 8;     // GDI+引用开销
            double memory = (bmpBytes * 1F + objectOverhead + gdiOverhead) / 1024 / 1024;
            this.packageSize = memory;
        }

        public void Dispose()
        {
            if (this.bmp != null)
            {
                this.bmp.Dispose();
                this.bmp = null;
                GC.Collect();
            }
        }
    }
}
